package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.ModularizationdCatDTO;
import com.yuanfeng.base.dto.ModularizationdCatDataDTO;
import com.yuanfeng.base.dto.ModularizationdGoodsDataDTO;
import com.yuanfeng.base.entity.BaseModularizationdCatEntity;
import com.yuanfeng.base.mapper.BaseModularizationdCatMapper;
import com.yuanfeng.base.service.BaseModularizationdCatService;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class BaseModularizationdCatServiceImpl extends ServiceImpl<BaseModularizationdCatMapper, BaseModularizationdCatEntity>
    implements BaseModularizationdCatService{

    @Override
    public ResponseResult selectAll(ModularizationdCatDTO param) throws IllegalAccessException {
        Map<String,Object> map =  new HashedMap();
        //获取传来的token
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + param.getToken());
        if(null == userInfoStr){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }

        Map data= ParseMapUtils.beanToMap(param);
        IPage<BaseModularizationdCatEntity> page=this.baseMapper.selectAllModularizationdCat(new Query<BaseModularizationdCatEntity>().getPage(data),
                param);

        return ResponseResult.success(PageUtils.getPage(page));

    }

    @Override
    public ResponseResult addModularizationdCat(ModularizationdCatDTO param) {
        //获取传来的token
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + param.getToken());
        if(null == userInfoStr){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        this.baseMapper.addModularizationdCat(param);
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult updateModularizationdCat(ModularizationdCatDTO param) {
        //获取传来的token
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + param.getToken());
        if(null == userInfoStr){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        this.baseMapper.updateModularizationdCat(param);
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult deleteModularizationdCat(ModularizationdCatDTO param) {
        //获取传来的token
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + param.getToken());
        if(null == userInfoStr){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        this.baseMapper.deleteById(param.getId());
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult updatePageApply(ModularizationdCatDTO param) {
        //获取传来的token
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + param.getToken());
        if(null == userInfoStr){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        //修改所有页面为为使用状态
        this.baseMapper.update(null,new LambdaUpdateWrapper<BaseModularizationdCatEntity>()
                        .set(BaseModularizationdCatEntity::getPageStart,2));
        //将前端传入的页面设置为使用中
        //页面状态 1 使用中2 未使用

        this.baseMapper.update(null,new LambdaUpdateWrapper<BaseModularizationdCatEntity>()
                             .set(BaseModularizationdCatEntity::getPageStart,param.getPageStart())
                             .eq(BaseModularizationdCatEntity::getId,param.getId()));
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult querPageDataById(ModularizationdCatDTO ModularizationdCatDTO) {
        BaseModularizationdCatEntity modularizationdCat =this.baseMapper.selectById(ModularizationdCatDTO.getId());
        return ResponseResult.success(modularizationdCat);
    }

    @Override
    public ResponseResult analysisPageData(ModularizationdCatDataDTO ModularizationdCatDTO) {
        if(null != ModularizationdCatDTO ){
            //解析补充 商品数据
            analysisGoodsData(ModularizationdCatDTO);
            //判断是否有下级 补充下级商品数据
            if(null != ModularizationdCatDTO.getModularizationdCatDataList() && ModularizationdCatDTO.getModularizationdCatDataList().size() > 0){
                List<ModularizationdCatDataDTO> modularizationdCatDataList = ModularizationdCatDTO.getModularizationdCatDataList();
                for (ModularizationdCatDataDTO modularizationdCatData : modularizationdCatDataList) {
                    analysisGoodsData(modularizationdCatData);
                }
            }
        }
        return ResponseResult.success(ModularizationdCatDTO);
    }

    @Override
    public ResponseResult queryApplyPage() {
        BaseModularizationdCatEntity entity=this.baseMapper.selectOne(new LambdaQueryWrapper<BaseModularizationdCatEntity>()
                                            .eq(BaseModularizationdCatEntity::getPageStart,1));
        return ResponseResult.success(entity);
    }


    public void analysisGoodsData(ModularizationdCatDataDTO modularizationdCatParam) {
        //查询商品对象中 商品数据
        if (null != modularizationdCatParam.getGoodsDataList() && modularizationdCatParam.getGoodsDataList().size() > 0) {
            List<ModularizationdGoodsDataDTO> goodsDataList = modularizationdCatParam.getGoodsDataList();
            for (ModularizationdGoodsDataDTO modularizationdGoodsData : goodsDataList) {
                ModularizationdGoodsDataDTO modularizationdGoodsInfo = this.baseMapper.queryGoodsInfo(modularizationdGoodsData);
                modularizationdGoodsData.setCommonId(modularizationdGoodsInfo.getCommonId());
                modularizationdGoodsData.setGoodsId(modularizationdGoodsInfo.getGoodsId());
                modularizationdGoodsData.setCommonImage(modularizationdGoodsInfo.getCommonImage());
                modularizationdGoodsData.setCommonName(modularizationdGoodsInfo.getCommonName());
                modularizationdGoodsData.setCommonPrice(modularizationdGoodsInfo.getCommonPrice());
            }
            //数据回填
            modularizationdCatParam.setGoodsDataList(goodsDataList);
        }
    }
}




